<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="关联井盖ID" prop="coverId" label-width="100">
        <el-input
          v-model="queryParams.coverId"
          placeholder="请输入关联井盖ID"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="工单类型" prop="orderType" label-width="100">
        <el-select v-model="queryParams.orderType" placeholder="请选择工单类型" clearable>
          <el-option
            v-for="dict in order_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="工单状态" prop="orderStatus" label-width="100">
        <el-select v-model="queryParams.orderStatus" placeholder="请选择工单状态" clearable>
          <el-option
            v-for="dict in order_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item> -->
      <br>
      <el-form-item label="维修负责人ID" prop="assigneeId" label-width="100">
        <el-input
          v-model="queryParams.assigneeId"
          placeholder="请输入维修负责人ID"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="上报人/巡检员ID" prop="inspectorId" label-width="120">
        <el-input
          v-model="queryParams.inspectorId"
          placeholder="请输入上报人/巡检员ID"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
      <hr>
    </el-form>
    
    <br>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Edit"
          @click="handle0"
          v-hasPermi="['managent:order:list']"
        >待派单</el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Pointer"
          @click="handle1"
          v-hasPermi="['managent:order:list']"
        >已派单</el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="Clock"
          @click="handle2"
          v-hasPermi="['managent:order:list']"
        >已接单</el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Check"
          @click="handle4"
          v-hasPermi="['managent:order:list']"
        >已完成</el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button
          type="error"
          plain
          icon="Close"
          @click="handle5"
          v-hasPermi="['managent:order:list']"
        >已关闭</el-button>
      </el-col>
      
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="orderList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="工单ID" align="center" prop="id" />
      
      <el-table-column label="工单类型" align="center" prop="orderType">
        <template #default="scope">
          <dict-tag :options="order_type" :value="scope.row.orderType"/>
        </template>
      </el-table-column>
      <el-table-column label="工单状态" align="center" prop="orderStatus">
        <template #default="scope">
          <dict-tag :options="order_status" :value="scope.row.orderStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="优先级" align="center" prop="priority">
        <template #default="scope">
          <dict-tag :options="priority" :value="scope.row.priority"/>
        </template>
      </el-table-column>
      <el-table-column label="图片" align="center" prop="image" width="100">
        <template #default="scope">
          <image-preview :src="scope.row.image" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="问题描述" align="center" prop="orderDesc"/>
      <el-table-column label="维修方案" align="center" prop="repairPlan"/>
      <el-table-column label="维修负责人" align="center" prop="assigneeId" />
      <el-table-column label="上报人/巡检员" align="center" prop="inspectorId" />
      <el-table-column label="处理管理员" align="center" prop="adminId" />
      <!-- <el-table-column label="实际完成时间" align="center" prop="completeTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.completeTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column> -->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="210">
        <template #default="scope">
          <el-button link type="primary" icon="Tickets" v-if="scope.row.orderStatus == 0" @click="handleEdit(scope.row)" v-hasPermi="['managent:order:edit']">派单</el-button>
          <!-- <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['managent:order:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['managent:order:remove']">删除</el-button> -->
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改工单主对话框 -->
    <el-dialog :title="title" v-model="express" width="500px" append-to-body>
      <el-form ref="expressRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="图片" prop="image">
          <image-preview :src="form.image" :width="200" :height="200"/>
        </el-form-item>
        <br>
        <el-form-item label="问题描述" prop="orderDesc">
          <el-input v-model="form.orderDesc" placeholder="请输入问题描述" />
        </el-form-item>

        <el-form-item label="选择工人" prop="assigneeId">
          <el-select :data=workers v-model="form.assigneeId" placeholder="请选择工人">
            <el-option
              v-for="worker in workers"
              :key="worker.id"
              :label="worker.userName"
              :value="worker.userId"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="优先级" prop="priority">
          <el-select v-model="form.priority" placeholder="请选择优先级">
            <el-option
              v-for="dict in priority"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitFormExpress">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>

    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="orderRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="关联井盖ID" prop="coverId">
          <el-input v-model="form.coverId" placeholder="请输入关联井盖ID" />
        </el-form-item>

        <el-form-item label="工单类型" prop="orderType">
          <el-select v-model="form.orderType" placeholder="请选择工单类型">
            <el-option
              v-for="dict in order_type"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        
        <el-form-item label="优先级" prop="priority">
          <el-select v-model="form.priority" placeholder="请选择优先级">
            <el-option
              v-for="dict in priority"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        


        <el-form-item label="上报人/巡检员ID" prop="inspectorId">
          <el-input v-model="form.inspectorId" placeholder="请输入上报人/巡检员ID" />
        </el-form-item>
      </el-form>

      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancelExpress">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Order">
import { listWorker, listOrder, getOrder, delOrder, addOrder, updateOrder } from "@/api/managent/order";

const { proxy } = getCurrentInstance();
const { order_status, priority, order_type } = proxy.useDict('order_status', 'priority', 'order_type');

const orderList = ref([]);
const open = ref(false);
const express = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const workers = ref([]);

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    coverId: null,
    orderType: null,
    orderStatus: '0',
    priority: null,
    orderDesc: null,
    repairPlan: null,
    assigneeId: null,
    inspectorId: null,
    adminId: null,
    completeTime: null,
    isDeleted: null
  },
  rules: {
    coverId: [
      { required: true, message: "关联井盖ID不能为空", trigger: "blur" }
    ],
    orderType: [
      { required: true, message: "工单类型不能为空", trigger: "change" }
    ],
    orderStatus: [
      { required: true, message: "工单状态不能为空", trigger: "change" }
    ],
    inspectorId: [
      { required: true, message: "上报人/巡检员ID不能为空", trigger: "blur" }
    ],
    createTime: [
      { required: true, message: "创建时间不能为空", trigger: "blur" }
    ],
    isDeleted: [
      { required: true, message: "删除标记不能为空", trigger: "blur" }
    ]
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询工单主列表 */
function getList() {
  loading.value = true;
  listOrder(queryParams.value).then(response => {
    orderList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

function getWorkList(){
  listWorker().then(response => {
    workers.value = response.data;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

function cancelExpress() {
  express.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    coverId: null,
    orderType: null,
    orderStatus: null,
    priority: null,
    orderDesc: null,
    repairPlan: null,
    assigneeId: null,
    inspectorId: null,
    adminId: null,
    createTime: null,
    updateTime: null,
    completeTime: null,
    isDeleted: null
  };
  proxy.resetForm("orderRef");
  proxy.resetForm("expressRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

function handle(){
  queryParams.value.orderStatus = '';
  getList();
}

function handle0(){
  queryParams.value.orderStatus = 0;
  getList();
}

function handle1(){
  queryParams.value.orderStatus = 1;
  getList();
}

function handle2(){
  queryParams.value.orderStatus = 2;
  getList();
}

function handle4(){
  queryParams.value.orderStatus = 4;
  getList();
}

function handle5(){
  queryParams.value.orderStatus = 5;
  getList();
}


/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加工单主";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getOrder(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改工单";
  });
}

function handleEdit(row) {
  reset();
  getWorkList();
  const _id = row.id || ids.value
  getOrder(_id).then(response => {
    form.value = response.data;
    express.value = true;
    title.value = "派发工单";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["orderRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateOrder(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addOrder(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

function submitFormExpress() {
  proxy.$refs["expressRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateOrder(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          express.value = false;
          getList();
        });
      } else {
        addOrder(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          express.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除工单主编号为"' + _ids + '"的数据项？').then(function() {
    return delOrder(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('managent/order/export', {
    ...queryParams.value
  }, `order_${new Date().getTime()}.xlsx`)
}

getList();
</script>
